/**
 * Created by trump on 15/11/30.
 */
var fs = require('fs-extra');
var path = require('path');
var webpack = require('webpack');
var _ = require('lodash');
var projectDir = __dirname;
var resolveFilePath = function (file) {
    return path.resolve(projectDir, file)
};
var configJSON = function () {
    var config = require('./config.json');
    try {
        _.assign(config, require('./config.dev.json'));
    } catch (E) {

    }
    return config;
}();

var entry = {};

var jsRelDir = './src/js';

fs.readdirSync(path.resolve(projectDir, jsRelDir)).forEach(function (project) {
    entry[project] = path.resolve(jsRelDir, project, 'main.jsx');
});

var HtmlWebpackPlugin = require('html-webpack-plugin');

var config = {
    entry: entry
    , devtool: 'source-map'
    , output: {
        path: '../build'
        , filename: '[name].js'
        , sourceMapFilename: '[file].map'
        , pathinfo: true
        , publicPath: (configJSON.serverPath || '') + (configJSON.publicPath || '') + '/'
    }
    , module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader'
            }
        ]
    }
    , plugins: [
        new HtmlWebpackPlugin({
            title: 'Yindoc',
            template: './src/templates/yindoc_app/index.html',
            scriptList: Object.keys(entry),
            publicPath: configJSON.publicPath,
            serverPath: configJSON.serverPath,
            devServerEnabled: configJSON.devServerEnabled
        })
    ]
};

module.exports = config;